﻿using DigDes.DSchool.SUPS.DataAccess.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace DigDes.DSchool.SUPS.DataAccess.FileLoaders
{
    public class MyLoader1 : ILoader<MyLoader1DTO>
    {
        // TODO: Реализовать интерфейс загрузчика и логику проверки типа файла/лоигку загрузки файла

        public bool Check(string filePath)
        {
            System.IO.FileInfo file = new System.IO.FileInfo(filePath);
            if (file.Extension == ".OUT")
                return true;
            else
            {
                throw new TypeLoadException();
            }
        }

        public IEnumerable<MyLoader1DTO> Load(string filePath)
        {
            using (StreamReader sr = new StreamReader(filePath, Encoding.GetEncoding("windows-1251")))
            {
                String line;
                while ((line = sr.ReadLine()) != null)
                {
                    var row = new MyLoader1DTO();

                    row.InventoryNumber = line.Substring(0, 8);
                    row.Weight = line.Substring(8, 3);
                    row.Destination = line.Substring(11, 5);
                    row.GoodCode = line.Substring(16, 5);
                    row.ReceiverCode = line.Substring(21, 4);
                    row.ParkType = line.Substring(25, 2);
                    row.StartStation = line.Substring(27, 5);
                    row.OperationCode = line.Substring(32, 2);
                    row.OperationDate = line.Substring(34, 4);
                    row.OperationYear = line.Substring(38, 2);
                    row.OperationTime = line.Substring(40, 4);
                    row.StationOfOperation = line.Substring(44, 5);
                    row.DeliveryRoad = line.Substring(49, 2);
                    row.ReceptionRoad = line.Substring(51, 2);
                    row.CoachIndex = line.Substring(53, 13);
                    row.CoachNumber = line.Substring(66, 4);
         
                    yield return row;
                }
            }
           
        }
        
    }
}
